package 题目集.前缀和.差分;

import java.util.Scanner;

public class 小孩子分糖果 {
    static int n;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        int[] nums = new int[n + 1];
        for (int i = 0; i < n; i++) {
            int v = sc.nextInt();
            nums[i] = v;
        }
        int res = 0;
        while (true) {
            boolean flag = true;
            for (int i = 0; i < n; i++) {
                if (nums[i] != nums[0]) {
                    flag = false;
                    break;
                }
            }
            if (flag) break;
            nums[n] = nums[0];
            for (int i = 0; i < n; i++) {
                nums[i] = nums[i] / 2 + nums[i + 1]/2;
            }
            for (int i = 0; i < n; i++) {
                if (nums[i] % 2 == 1) {
                    nums[i]++;
                    res++;
                }
            }
        }
        System.out.println(res);
    }
}
